#!/bin/bash

source ~/command/common
ip="localhost"
user=root
port=22
password=admin
default_remote_root=/root/
_oper=0
_name=""
_localpath="./"
_remote_root=""
_remotepath=""

function setclipboard() {
    echo
}
function xssh_enter() {
	if type expect >/dev/null 2>&1; then
		expect -c "
		set timeout 30000;
		spawn ssh ${user}@${ip} -p ${port};
		expect {
			\"*password:\" {send \"${password}\r\"; exp_continue}
			\"*#\" {send \" cd  ${_remote_root}\r\"}
		}
		interact
		"
	else
		echo "密码: ${password}"
		echo "命令: cd ${_remote_root}"
		echo ssh ${user}@${ip} -p ${port}
		setclipboard "${password}"
		ssh ${user}@${ip} -p ${port}
	fi
}
function xssh_push() {
	[ -z "${_localpath}" ]&&return
	if type expect >/dev/null 2>&1; then
		expect -c "
		set timeout 30000;
		spawn scp -P ${port}  -r "${_localpath}" "${user}@${ip}:${_remotepath}"
		expect {
			\"*password:\" {send \"${password}\r\"}
		}
		interact
		"
	else
		echo "密码: ${password}"
		echo scp -P ${port}  -r "${_localpath}" "${user}@${ip}:${_remotepath}"
		setclipboard "${password}"
		scp -P ${port}  -r "${_localpath}" "${user}@${ip}:${_remotepath}"
	fi
}
function xssh_pull() {
	[ -z "${_localpath}" ]&&return
	if type expect >/dev/null 2>&1; then
		expect -c "
		set timeout 30000;
		spawn scp -P ${port}  -r "${user}@${ip}:${_remotepath}" "${_localpath}"
		expect {
			\"*password:\" {send \"${password}\r\"}
		}
		interact
		"
	else
		echo "密码: ${password}"
		echo scp -P ${port}  -r "${user}@${ip}:${_remotepath}" "${_localpath}"
		setclipboard "${password}"
		scp -P ${port}  -r "${user}@${ip}:${_remotepath}" "${_localpath}"
	fi
}
function show_help() {
	local -a list
	list="xssh"
	list=("${list[@]}" "Usage: xssh [-s|-l|-h]")
	list=("${list[@]}" "xssh: login ssh")
	list=("${list[@]}" "xssh -s [local:remote]: push local file/dir to ssh server")
	list=("${list[@]}" "xssh -l [remote:local]: pull remote file/dir from ssh server to local")
	list=("${list[@]}" "xssh -c [config file]: set config file")
	list=("${list[@]}" "xssh -i: ip")
	list=("${list[@]}" "xssh -p: port")
	list=("${list[@]}" "xssh -u: user")
	list=("${list[@]}" "xssh -w: password")
	list=("${list[@]}" "xssh -h: show help")
	__msgbox "${list[@]}"
}
function main() {
	while getopts :s:l:c:p:w:u:mti:r:h opt;do
		case ${opt} in
			s)_oper=1;_localpath="${OPTARG%%:*}";_remotepath="${OPTARG##*:}";;
			l)_oper=2;_remotepath="${OPTARG%%:*}";_localpath="${OPTARG##*:}";;
			m)_config_file="${HOME}/account/mac_config";;
			t)_config_file="${HOME}/account/shen_ou_config";;
			c)_config_file="$OPTARG";;
			p)port="$OPTARG";;
			i)ip="$OPTARG";;
			u)user="$OPTARG";;
			w)password="$OPTARG";;
			r)_remote_root="$OPTARG";;
			*)show_help;exit;;
		esac
	done

	if [ -n "${_config_file}" ];then
		ip=`sed '/^public_ip=/!d;s/.*=//' ${_config_file}`
		password=`sed '/^password=/!d;s/.*=//' ${_config_file}`
		port=`sed '/^port=/!d;s/.*=//' ${_config_file}`
		user=`sed '/^user=/!d;s/.*=//' ${_config_file}`
		_remote_root=`sed '/^root=/!d;s/.*=//' ${_config_file}`
	fi
	[ -z "${port}" ]&&port=22
	[ -z "${user}" ]&&user=root
	[ -z "${_remote_root}" ]&&_remote_root="${default_remote_root}"
	[ "${_remote_root%%/}" = "$_remote_root" ]&&_remote_root="${_remote_root}/"

	if ! [ "${_localpath:0:1}" = "/" -o "${_localpath:0:1}" = "~" ];then
		_localpath="./${_localpath}"
	fi
	if ! [ "${_remotepath:0:1}" = "/" -o "${_remotepath:0:1}" = "~" ];then
		_remotepath="${_remote_root}${_remotepath}"
	fi

	case ${_oper} in
		0)xssh_enter;;
		1)xssh_push;;
		2)xssh_pull;;
	esac
}

main "$@"
